package com.fr.lintcode;

import org.junit.Test;

/**
*作者：furong
*日期：2017年1月22日
*时间：下午5:24:18
*/
public class Q115 {
    /**
     * @param obstacleGrid: A list of lists of integers
     * @return: An integer
     */
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if (obstacleGrid == null || obstacleGrid[0] == null || obstacleGrid[0][0] == 1) {
            return 0;
        }
        int n = obstacleGrid.length;
        int m = obstacleGrid[0].length;
        int count[][] = new int[n][m];
        count[0][0] = 1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (obstacleGrid[i][j] == 1) {
                    count[i][j] = 0;
                } else if (i == 0 && j == 0) {
                    continue;
                } else if (i == 0) {
                    count[i][j] = count[i][j - 1];
                } else if (j == 0) {
                    count[i][j] = count[i - 1][j];
                } else {
                    count[i][j] = count[i][j - 1] + count[i - 1][j];
                }
            }
        }
        return count[n - 1][m - 1];
    }

    @Test
    public void testA() {
        int[][] obstacleGrid = { { 0, 0, 0 }, { 0, 1, 0 }, { 0, 0, 0 } };
        System.out.println(uniquePathsWithObstacles(obstacleGrid));
    }
}
